<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px;">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6">
              <a-form-item label="对账单编号">
                <a-input v-model="queryParam.accountCode" placeholder="请输入对账单编号"/>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="客户名称">
                <a-input v-model="queryParam.customerName" placeholder="请输入客户名称"/>
              </a-form-item>
            </a-col>
            
            <a-col :span="6">
              <a-form-item label="费用类型">
                <a-select placeholder="请选择费用类型" v-model="queryParam.costType" style="width: 100%" allow-clear >
                  <a-select-option v-for="(d, index) in inOutOptions" :key="index" :value="d.dictValue">{{
                    d.dictLabel
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right;">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 收款 -->
    <biz-income-payment-add-form v-if="showAddModal" ref="bizIncomePaymentAddForm" :payOptions="payOptions" @ok="getList" @close="showAddModal = false" />
      <advance-table
      :customRow="customRow"
      :components="drag(columns)"
       title="收支单" :pagination="{
        current: queryParam.pageNum,
        pageSize: queryParam.pageSize,
        total: total,
        showSizeChanger: true,
        showLessItems: true,
        showQuickJumper: true,
        pageSizeOptions:pageSizeOptions,
        showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
        onChange: changeSize,
        onShowSizeChange: onShowSizeChange
      }" tableKey="base-bizOrder-index-table" @change="handleTableChange" rowKey="id" @refresh="getList"
        :row-selection="{ selectedRowKeys: selectedRowKeys,getCheckboxProps:getCheckboxProps, onChange: onSelectChange,type:'radio' }"
        :columns="columns" :data-source="dataList" :loading="loading" :format-conditions="true">
        
        <div class="table-operations" slot="button">
          <a-button type="primary"  @click='handlePayment({})' v-hasPermi="['biz:bizCost:add']">
            <a-icon />新增
          </a-button>
          <a-button type="primary" v-hasPermi="['biz:bizPayment:export']" :loading="downLoading"  @click='exportExcel'>
            <a-icon />导出
          </a-button>
          <!-- <a-button type="primary" :disabled="!selectedRows.length" @click="exportExcel('对账明细')"  v-hasPermi="['biz:bizPayment:detailExport']">
                    导出
          </a-button> -->
        </div>
        
        <span slot="operation" slot-scope="{record}" v-if="record.id">
          <a @click="handlePayment(record,0)" v-hasPermi="['biz:bizCost:query']">
            明细
          </a>
          <span v-if="record.status == 0" v-hasPermi="['biz:bizCost:edit']">
            <a-divider type="vertical" />
            <a @click="handlePayment(record,0)"  >
              修改
            </a>
          </span>
          <a-divider type="vertical" v-if="record.status === '0'"/>
          <a  v-hasPermi="['biz:bizCost:remove']" @click="handlePaymentRemove(record)" v-if="record.status === '0'">
            删除
          </a>
          <a-divider type="vertical" />
          <a type="primary" @click="audit(record,1)" v-if="record.status == 0" v-hasPermi="['biz:bizCost:audit']">
            审核
          </a>
          <a type="primary" @click="audit(record,0)" v-if="record.status > 0" v-hasPermi="['biz:bizCost:audit']">
            反审
          </a>
        </span>
        <span slot="statusSlot" slot-scope="{record}" v-if="record.id">
          <span v-if="record.status == 0">未审核</span>
          <span v-else-if="record.status == 1">已审核</span>
        </span>
        <span slot="costTypeSlot" slot-scope="{record}">
          <span v-if="record.costType === '0'">支出</span>
          <span v-else-if="record.costType === '1'">收入</span>
          <span v-else-if="!record.id">合计</span>
        </span>

        
      </advance-table>
    </a-card>
    
    <ant-modal
      :visible="open"
      modal-title="打印采购明细表"
      :adjust-size="false"
      @cancel="cancel"
      modalHeight="550"
      modalWidth="1000">
      <a-row slot="content">
        <iframe height='425' width='985' style='border:none;' :src='printUrl' ></iframe>
      </a-row>

      <div slot="footer">
        <a-button @click="cancel">关闭</a-button>
      </div>
    </ant-modal>
  </div>
</template>
<script>
import { listBizCustomerAccount,delBizCustomerAccount ,exportBizCustomerAccountDetail } from '@/api/biz/bizCustomerAccount'
import {listBizCost,delBizCost,bizCostAudit} from '@/api/biz/bizIncomePayment';
import { getInitData } from '@/api/biz/bizPurchase'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import BizIncomePaymentAddForm from '@/views/biz/bizFeeManagement/modules/BizIncomePaymentAddForm.vue'
import tableDragResizeMixin from '@/mixins/tableDragResizeMixin'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import AntModal from '@/components/pt/dialog/AntModal'
import storage from 'store'

import listMixin from '@/mixins/listMixin';
export default {
  name: 'bizIncomePayment',
  components: {
    AdvanceTable,
    BizIncomePaymentAddForm,
    AntModal
  },
  mixins:[tableDragResizeMixin,listMixin],
  data() {
    return {
      // 打印弹窗
      open:false,
      printUrl:'/api/biz/bizOrderProcessRecord/export?Authorization='+storage.get(ACCESS_TOKEN),
      printUrlPath:'/api/biz/bizOrderProcessRecord/export?Authorization='+storage.get(ACCESS_TOKEN),
      showAddModal: false,
      title:'收支单',
      listFunc:(param)=>listBizCost(param),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 表格数据
      dataList: [],
      payOptions: [],
      inOutOptions:[],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 11,
        accountCode: undefined,
        customerName: undefined,
        costType:'1'
      },
      
      pageSizeOptions: ['11', '21', '31', '101', '201'],
      columns: [
        {
          title: '费用类型',
          dataIndex:'costType',
          align: 'center',
          scopedSlots: { customRender: 'costTypeSlot' }

        },
        {
          title: '编号',
          dataIndex:'payCode',
          align: 'center'
        },
        {
          title: '日期',
          dataIndex:'payDate',
          align: 'center'
        },
        {
          title: '操作员',
          dataIndex:'createByName',
          align: 'center'
        },
        {
          title: '金额',
          dataIndex:'payAmount',
          align: 'center'
        },
        {
          title: '备注',
          dataIndex:'remark',
          align: 'center'
        },
        {
          title: '状态',
          dataIndex:'status',
          align: 'center',
          scopedSlots: { customRender: 'statusSlot' }
        },
        {
          title: '操作',
          align: 'center',
          width: '10%',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created() {
    
    this.getList()
    getInitData('pay_type,in_out_type').then(response => {
      this.payOptions = response.data.pay_type
      this.inOutOptions = response.data.in_out_type
    })
  },
  methods: {
    
    
    /** 导出按钮操作 */
    // exportExcel () {
    //   var that = this,param
    //   if(this.selectedRows.length){
    //     param = {
    //       accountCode:this.selectedRows[0].accountCode
    //     }
    //   }else{
    //     return this.$message.error('请选择一条数据')
    //   }
    //   this.$confirm({
    //     title: '是否确认导出?',
    //     content: '此操作将导出当前条件下所有数据而非选中数据',
    //     onOk () {
    //       return exportBizCustomerAccountDetail(param)
    //         .then(response => {
    //           that.download(response.msg)
    //           that.$message.success(
    //             '导出成功',
    //             3
    //           )
    //       })
    //     },
    //     onCancel () {}
    //   })
    // },
    /** 查询销售单列表 */
    getList() {
      this.loading = true
      let param = this.getQueryParam()
      listBizCost(param).then(response => {
        this.dataList = response.data.list
        this.total = response.data.total
        this.loading = false
        this.tableAddTotalRow(this.columns,this.dataList)
      })
    },
    loadAllData(){
      return new Promise((resolve, reject) => {
        let param = this.getQueryParam()
        param.pageSize = this.total
        this.listFunc(param).then(res=>{
          if(res.code == 200) {
            let arr = res.data.list || []
            arr = arr.map(item => {
              item.status = item.status == 0 ? '未审核' : '已审核'
              item.costType = item.costType == 0 ? '支出' : '收入'
              return item
            })
            resolve(arr)
          }else{
            this.message.error(res.msg)
            reject(res.msg)
          }
        })
      })
    },
    handlePayment(row,type = null){
      this.showAddModal = true
      this.$nextTick(() => {
        let data = {customerId:row.customerId,customerName:row.customerName,accountCode:row.accountCode,totalAmount:row.totalAmount};
        row.queryType = type
        this.$refs.bizIncomePaymentAddForm.handlePayment(row)
      })
    },
    handlePaymentRemove(row){
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk: () => {
          delBizCost(row.id).then(res=>{
            if(res.code == 200) {
              this.$message.success(
                '删除成功',
                3
              )
              this.getList()
            }else{
              this.message.error(res.msg)
            }
          })
        }
      })
    },
    cancel(){
      this.open = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        supplierName: undefined

      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    handleTableChange(pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    },
    
    customRow (record) {
        return {
          on: {
            dblclick: () => {
              this.$nextTick(() => {
                if(record.id) this.handlePayment(record,0)
              })
            }
          }
        }
    },
    
    audit(row, status) {
      var that = this
      that.$confirm({
        title: `确认${status == 0 ? '反' : ''}审核所选中数据?`,
        onOk() {
          return bizCostAudit(row.id, status).then((res) => {
            if (res.code === 200) {
              that.$message.success('更新成功', 3)
              that.getList()
            }
          }).catch(()=>{

          })
        },
        onCancel() { },
      })
    },

  }
}
</script>
